#include "sqltest.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QStandardItemModel>
#include <src/commom/staticbutton.h>
#include <dirent.h>
#include <QCoreApplication>
#include <direct.h> //
#include <QTextStream>

sqlTest::sqlTest(QWidget *parent) : BaseStyleWidget(parent)//继承父类才能设置背景颜色
{
    dbPage = new DbPage(this);
    this->initUI();
    this->initConnect();
    this->setStyleSheet("QWidget{background:white;}");
    this->initForm();
    on_btnSelect_clicked();
}
void sqlTest::initUI()
{
//    m_safeButton = new StaticButton(":/main/safe_check");
//    QLabel *safeLabel = new QLabel(tr("查杀修复"));
//    safeLabel->setObjectName("mainBottomLabel");
//    QVBoxLayout *safeLayout = new QVBoxLayout;
//    safeLayout->addWidget(m_safeButton, 0, Qt::AlignCenter);
//    safeLayout->addWidget(safeLabel, 0, Qt::AlignCenter);

    m_stacked = new QStackedWidget;
    m_stacked2 = new QStackedWidget;
    m_stacked3 = new QStackedWidget;
    m_stacked4 = new QStackedWidget;
    m_stacked5 = new QStackedWidget;
    Button = new StaticButton(":/sql/1");
    Button2 = new StaticButton(":/sql/2");
    Button3 = new StaticButton(":/sql/3");
    Button4 = new StaticButton(":/sql/4");
    Button5 = new StaticButton(":/sql/5");
//            Button = new QPushButton("查询");
//            Button2 = new QPushButton("上一页");
//            Button3 = new QPushButton("下一页");
//            Button4 = new QPushButton("第一页");
//            Button5 = new QPushButton("末一页");
    Label= new QLabel;
    Label2= new QLabel;
    Label3= new QLabel;
    Label4= new QLabel;
    Label5= new QLabel;
    Label6= new QLabel;
    Label7= new QLabel(this);
    Label->setText("1");
    Label2->setText("12");
    Label3->setText("13");
    Label4->setText("15");
    Label5->setText("16");
    Label6->setText("17");
    Label6->hide();
    Label7->hide();
    m_stacked->addWidget(Button);
    m_stacked2->addWidget(Button2);
    m_stacked3->addWidget(Button3);
    m_stacked4->addWidget(Button4);
    m_stacked5->addWidget(Button5);

    m_view = new QTableView(this);
    m_view->setStyleSheet("QTableView { background-color: white; border: 1px solid white; }");
    m_view->setFixedSize(700, 800);

   //  4. 设置字体大小（这只会影响表头）
    QFont font ;
    font.setPointSize(10);
    m_view->horizontalHeader()->setFont(font);
    m_view->verticalHeader()->setFont(font); // 如果你也有垂直表头的话

    // 显示表格视图
//    m_view->show();

    QGridLayout *mainLayout = new QGridLayout;
//    mainLayout->addWidget(m_view, 0,0, Qt::AlignCenter);
//    mainLayout->addWidget(m_view, 0, 0, 8, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(m_stacked, 0, 1, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(m_stacked2, 2, 8, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(m_stacked3, 3, 8, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(m_stacked4, 4, 8, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(m_stacked5, 5, 8, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(Label, 10, 8, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(Label3, 7, 1, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(Label4, 8, 1, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(Label5, 9, 1, 1, 1); // (row, column, rowSpan, columnSpan)
//    mainLayout->addWidget(Label6, 10, 1, 1, 2); // (row, column, rowSpan, columnSpan)

    //    layout->addWidget(button, i, 3, 1, 2); // 添加到第i行，从第3列开始，跨越1行2列
    mainLayout->addWidget(m_stacked, 0,1, Qt::AlignRight);
    mainLayout->addWidget(m_stacked2, 1,1, Qt::AlignRight);
    mainLayout->addWidget(m_stacked3, 2,1, Qt::AlignRight);
    mainLayout->addWidget(m_stacked4, 3,1, Qt::AlignRight);
    mainLayout->addWidget(m_stacked5, 4,1, Qt::AlignRight);
    mainLayout->addWidget(Label, 5,1, Qt::AlignRight);
    mainLayout->addWidget(Label2, 6,1, Qt::AlignRight);
    mainLayout->addWidget(Label3, 7,1, Qt::AlignRight);
    mainLayout->addWidget(Label4, 8,1, Qt::AlignRight);
    mainLayout->addWidget(Label5, 9,1, Qt::AlignRight);
    mainLayout->addWidget(Label6, 10,1, Qt::AlignRight);
    mainLayout->addWidget(Label7, 10,1, Qt::AlignRight);

    this->setLayout(mainLayout);
}
void sqlTest::initConnect()
{
    connect(Button, SIGNAL(buttonClicked()), this, SLOT(on_btnSelect_clicked()));
    connect(Button2, SIGNAL(buttonClicked()), this, SIGNAL(viewClicked()));
    connect(Button3, SIGNAL(buttonClicked()), this, SIGNAL(payinsureClicked()));
        connect(Button4, SIGNAL(buttonClicked()), this, SIGNAL(securityClicked()));
}

void sqlTest::initForm()
{
    columnNames.clear();
    columnWidths.clear();

    tableName = "dataconM";
    countName = "rowid";

    columnNames.append("序号");
    columnNames.append("时间");
    columnNames.append("NO(PPM)");
    columnNames.append("NO2(PPM)");
    columnNames.append("NOx(PPM)");
    columnNames.append("流量");
    columnNames.append("压力");
    columnNames.append("反应室温度");
    columnNames.append("机箱温度");
    columnNames.append("冷堆温度");
    columnNames.append("转换炉温度");
    columnNames.append("PMT电压");
//    columnNames.append("PMT频率");
//    columnNames.append("臭氧流量");



    columnWidths.append(50);
    columnWidths.append(130);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
    columnWidths.append(80);
//    columnWidths.append(80);

    //设置需要显示数据的表格和翻页的按钮
    //设置所有列居中显示
    dbPage->setAllCenter(true);


    dbPage->setControl(m_view,Label, Label3, Label2, Label4,
                       Label5, Label6, Button4, Button2, Button3, Button5, countName);

    m_view->horizontalHeader()->setStretchLastSection(true);
    m_view->verticalHeader()->setDefaultSectionSize(25);

}

void sqlTest::setStackedButton(int index)
{
    m_stacked->setCurrentIndex(index);
}

void sqlTest::cleanSql()
{
    QSqlQuery query; // 注意：这里不需要传递 dbConn，因为 query 默认使用默认连接
    QString sql = "DELETE FROM dataconM";
    if (!query.exec(sql)) {
        qDebug() << "删除失败:" ;
    }
    else {

        qDebug() << "删除成功:" ;
    }
    QString sql2 = "DELETE FROM sqlite_sequence WHERE name = 'dataconM';";
    query.exec(sql2);

   const QString filename ="C:/Users/ar/Downloads/Qt360li/123.txt";
    {
        QFile file(filename);
        if (file.open(QIODevice::WriteOnly)) {
            QTextStream stream(&file);
            stream << "时间, 浓度, 温度, ...\n";
            QSqlQuery query("SELECT * FROM dataconM");
            while (query.next()) {
                stream << query.value(0).toString() << ","
                       << query.value(1).toDouble() << ","
                       << query.value(2).toDouble() << "\n";
            }

        }

    }




    dbPage->select();



}

void sqlTest::on_btnSelect_clicked()
{
    //打开数据库,整个应用程序可用
    QSqlDatabase dbConn = QSqlDatabase::addDatabase("QSQLITE");
    dbConn.setDatabaseName("testsql.db");//当生成可执行文件时可以直接用这个


//            <<qApp->applicationDirPath();

    dbConn.setDatabaseName(qApp->applicationDirPath() + "/testsql.db");
    if (dbConn.open()) {
        qDebug() << "连接数据库成功!";
    } else {
        qDebug() << "连接数据库失败!";
    }
    //绑定数据到表格
    QString sql = "where 1=1";
    dbPage->setTableName(tableName);
    dbPage->setOrderSql(QString("%1 %2").arg(countName).arg("asc"));
    dbPage->setWhereSql(sql);
    dbPage->setRecordsPerpage(15);
    dbPage->setColumnNames(columnNames);
    dbPage->setColumnWidths(columnWidths);
    dbPage->select();
}
